<?php
namespace App\Modules\Admin\Services;

use App\Models\Admin\SysUser;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class UserService extends AdminService
{
    protected $model = SysUser::class;

    public function getQueryBuilder(Array $data)
    {
        $this->queryBuilder = SysUser::query();
        if(isset($data['username']) && strlen($data['username']) > 0) {
            $this->queryBuilder->where('username', $data['username']);
        }

        if(isset($data['sort_by']) && isset($data['order']) && in_array(Str::lower($data['order']), ['asc', 'desc'])) {
            $this->queryBuilder->orderBy($data['sort_by'], $data['sort_by']);
        } else {
            $this->queryBuilder->orderBy('id', 'desc');
        }
    }

    public function options()
    {
        return SysUser::select('id', 'name')->get();
    }

    public function add(Array $data)
    {
        $data['password'] = Hash::make($data['password']);
        return parent::add($data);
    }

    public function changePassword(Int $id, String $password)
    {
        return parent::edit($id, [
            'password' => Hash::make($password)
        ]);
    }

}